package com.itheima.controller;

import com.itheima.dao.CarDao;
import com.itheima.domain.Car;
import com.itheima.service.CarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@RestController
@RequestMapping("/cars")
public class CarController {
    @Autowired
    private CarService carService;

    @PostMapping
    public Result save(Car car) {
        boolean flag = carService.save(car);
        return new Result(flag?Code.SAVE_OK:Code.SAVE_ERR,flag);
    }
    @PutMapping
    public Result update(Car car) {
        boolean flag = carService.update(car);
        return new Result(flag?Code.UPDATE_OK:Code.UPDATE_ERR,flag);
    }

    @DeleteMapping("/{id}")
    public Result delete(Integer id) {
        boolean flag = carService.delete(id);
        return new Result(flag?Code.DELETE_OK:Code.DELETE_ERR,flag);
    }

    @GetMapping("/{id}")
    public Result getById(Integer id) {
        Car car = carService.getById(id);
        Integer code = car!=null?Code.GET_OK:Code.GET_ERR;
        String msg = car!= null ? " ":"数据查询有误,请重试";

        return new Result(code,car,msg);
    }

    @GetMapping
    public Result getAll() {
        List<Car> carList = carService.getAll();
        Integer code = carList!=null?Code.GET_OK:Code.GET_ERR;
        String msg = carList!= null ? " ":"数据查询有误,请重试";

        return new Result(code,carList,msg);    }
}
